<?php
/* Template Name: 投稿页
 * Author: 露兜
 * Modified: 缙哥哥
 */
?>
<?php get_header()?>
<div class="hide"><?php ini_map()?></div>
<?php
if( isset($_POST['tougao_form']) && $_POST['tougao_form'] == 'send'){
	global $wpdb;
	$last_post = $wpdb->get_var("SELECT post_date FROM $wpdb->posts WHERE post_type = 'post' ORDER BY post_date DESC LIMIT 1");

// 投稿距离最新发布文章间隔120秒
	if ( current_time('timestamp') - strtotime($last_post) < 120 ){
		wp_die('您投稿也太勤快了吧，先歇会儿！');
	}

// 表单变量初始化
	$name = isset( $_POST['tougao_authorname'] ) ? trim(htmlspecialchars($_POST['tougao_authorname'], ENT_QUOTES)) : '';
	$email =  isset( $_POST['tougao_authoremail'] ) ? trim(htmlspecialchars($_POST['tougao_authoremail'], ENT_QUOTES)) : '';
	$blog =  isset( $_POST['tougao_authorblog'] ) ? trim(htmlspecialchars($_POST['tougao_authorblog'], ENT_QUOTES)) : '';
	$title =  isset( $_POST['tougao_title'] ) ? trim(htmlspecialchars($_POST['tougao_title'], ENT_QUOTES)) : '';
	$category =  isset( $_POST['cat'] ) ? (int)$_POST['cat'] : 0;
	$tags = isset( $_POST['tougao_tags'] ) ? $_POST['tougao_tags'] : '';
	$content =  isset( $_POST['tougao_content'] ) ? $_POST['tougao_content'] : '';

// 表单项数据验证
	if ( empty($name) || mb_strlen($name) > 20 ){
		wp_die('昵称必须填写，长度不得超过20字 <a href="javascript:;" onclick=\'location.href="javascript:history.go(-1);"\'>返回填写</a>');
	}

	if ( empty($email) || strlen($email) > 60 || !preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $email)){
		wp_die('Email必须填写，须符合Email格式，且长度不得超过60字 <a href="javascript:;" onclick=\'location.href="javascript:history.go(-1);"\'>返回填写</a>');
	}

	if ( empty($title) || mb_strlen($title) > 100 ){
		wp_die('标题必须填写，长度不得超过100字 <a href="javascript:;" onclick=\'location.href="javascript:history.go(-1);"\'>返回填写</a>');
	}

	if ( empty($content) || mb_strlen($content) > 3000 || mb_strlen($content) < 100){
		wp_die('内容必须填写，且不得少于100字，不得超过3000字 <a href="javascript:;" onclick=\'location.href="javascript:history.go(-1);"\'>返回填写</a>');
	}

	$post_content = '昵称: '.$name.'<br />邮箱: '.$email.'<br />来源: '.$blog.'<br />内容:<br />'.$content;

	$tougao = array(
		'post_title' => $title,
		'post_content' => $post_content,
		'tags_input' => $tags,
		'post_category' => array($category)
	);

// 将文章插入数据库
	$status = wp_insert_post( $tougao );

	if ($status != 0) {
		// 投稿成功给博主发送邮件
		$subject = '[ '.get_bloginfo('name').' ] 有用户投稿';
		$message = '有用户到您的站点投稿，请<a href="'.get_bloginfo('url').'/wp-admin/edit.php" target="_blank">点此登录</a>审核。';
		add_filter('wp_mail_content_type',create_function('', 'return "text/html";'));
		wp_mail( get_bloginfo('admin_email'), $subject, $message );
		// 添加作者邮箱元数据
		add_post_meta($status, 'tougao_authoremail', $email , TRUE);

		wp_die('投稿成功！您的文章将在审核通过后发布！','投稿成功！');
	}
	else {
		wp_die('投稿失败！');
	}
}


//导航菜单
	$custom_fields = get_post_custom_keys($post_id);
	if( !in_array( 'pgmenu', $custom_fields) ){
		$pgmenu = 1;
	}
?>
<div class="main-page" <?php if( $pgmenu!==1 ) echo 'style="padding:20px 25px"'?>>
<?php if( $pgmenu==1 && !ismobile() ) wp_nav_menu( array('theme_location' => 'pagemenu', 'class' => 'pg-menu') )?>
		<h2 class="entry-title"><?php the_title()?></h2>
<?php while (have_posts()): the_post()?>
<span><?php edit_post_link('编辑文章','<i class="icon-edit"></i> ')?></span>
<div class="context tougao" property="v:description">
<?php the_content()?>
<form method="post" action="<?php echo $_SERVER["REQUEST_URI"]?>">
	<p>
		<label>昵称：
			<input type="text" size="30" value="" id="tougao_authorname" name="tougao_authorname" /> *
		</label>
	</p>

	<p>
		<label>邮箱：
			<input type="text" size="30" value="" id="tougao_authoremail" name="tougao_authoremail" onclick="$('.tip').fadeIn('fast');" onblur="$('.tip').fadeOut('fast');"/> *
		</label>
		<small class="tip hide">审核结果将通过邮件发送</small>
	</p>

	<p>
		<label>来源：
			<input type="text" size="30" value="" id="tougao_authorblog" name="tougao_authorblog" />
		</label>
	</p>

	<p>
		<label>分类：
			<?php wp_dropdown_categories('show_option_none=请选择分类&id=tougao-cat&hierarchical=1&hide_empty=0')?>
		</label>
	</p>

	<p>
		<label>标题：
			<input type="text" size="60" placeholder="标题必须填写" id="tougao_title" name="tougao_title"/>
		</label>
	</p>

	<p>
		<label>标签：</label>
		<input type="text" size="60" placeholder="请用顿号隔开" name="tougao_tags"/>
	</p>

	<div style="border:1px solid #DDD">
<?php
wp_editor(
	'', // Editor content.
		'tougao_content', // Editor ID.
		array(
			'tinymce' => true, // Use TinyMCE in a meta box.
			'quicktags' => false,
			'media_buttons' => false,
			'textarea_rows' => 15,
			'textarea_name' => 'tougao_content'
		)
);
?>
	</div>

	<p style="text-align:center">
		<input type="hidden" value="send" name="tougao_form" />
		<input type="reset" class="button" value=" 重填 " />　
		<input type="submit" class="button" value=" 提交 " />
	</p>
</form>
</div>
<?php endwhile?>
</div>
<?php get_footer()?>